package com.itboye.mengwu.app.ui.aiwujia;

import java.util.Observable;
import java.util.Observer;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.R.id;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.app.ui.mymeng.personsettings.AddressManageActivity;
import com.itboye.mengwu.app.ui.mymeng.personsettings.MaMaZengAddressManageActivity;
import com.itboye.mengwu.data.model.AddressQueryModel;
import com.itboye.mengwu.data.model.PersonDataBean;
import com.itboye.mengwu.data.presenter.AddressQueryPresenter;
import com.itboye.mengwu.data.presenter.NewMaMaZengPresenter;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ActivityShenQingXinXiTianXie extends BaseActivity implements Observer {

	String pid, uid, addressId;
	ImageView back;
	TextView txt_title, nickName, phoneNum, address;
	EditText et;
	RelativeLayout querenshenqingLine;

	AddressQueryPresenter addressQueryPresenter;
	NewMaMaZengPresenter newMaMaZengPresenter;

	PersonDataBean bean;
	LinearLayout addressMessageLine;
	private String peopleName;

	private String phone;
	private String addressStr;

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		System.out.println(entity.getData());
		if (entity != null) {
			if (entity.getEventType() == AddressQueryPresenter.AddressDefaultSuccessEvent) {

				bean = (PersonDataBean) entity.getData();
				if (TextUtils.isEmpty(uid)) {
					Intent intent = new Intent(ActivityShenQingXinXiTianXie.this, LoginActivity.class);
					startActivity(intent);
					finish();
				} else if (bean != null & bean.getId() != null & entity.getData() != null) {
					address.setText(bean.getProvince() + bean.getCity() + bean.getArea() + bean.getDetailinfo());
					nickName.setText(bean.getContactname());
					phoneNum.setText(bean.getMobile());

					addressId = bean.getId();
				} else if (bean == null || bean.getId() == null) {

					ByAlert.alert("请填写地址");
					Intent intent = new Intent(ActivityShenQingXinXiTianXie.this, AddressManageActivity.class);
					startActivity(intent);
					finish();
				}

			} else if (entity.getEventType() == AddressQueryPresenter.AddressDefaultFailEvent) {

			}

			if (entity.getEventType() == NewMaMaZengPresenter.ShenqingSuccess) {

				ByAlert.alert(entity.getData().toString());
				finish();

			} else if (entity.getEventType() == NewMaMaZengPresenter.ShenqingFail) {

			}
		}
	}

	@Override
	public int initView() {
		return R.layout.activity_mamazeng_shenqingxinxitianxie;
	}

	@Override
	public void onMyCreate() {
		Intent intent = getIntent();
		pid = intent.getStringExtra("pid");
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";

		if (TextUtils.isEmpty(uid)) {
			Intent intentLog = new Intent(ActivityShenQingXinXiTianXie.this, LoginActivity.class);
			startActivity(intentLog);
			finish();
		}

		addressQueryPresenter = new AddressQueryPresenter(this);
		addressQueryPresenter.getDefaultAddr(uid);
		newMaMaZengPresenter = new NewMaMaZengPresenter(this);
		txt_title.setText("申请信息填写");
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		querenshenqingLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (!TextUtils.isEmpty(et.getText())) {
					// Log.e("~~~~~~", addressId+"yyyy");
					newMaMaZengPresenter.shenqing(pid, uid, et.getText() + "", addressId);
				} else {
					ByAlert.alert("申请原因不能为空！");
				}

			}
		});

		addressMessageLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityShenQingXinXiTianXie.this, MaMaZengAddressManageActivity.class);

				startActivityForResult(intent, 101);

			
			}
		});

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, data);

		
		if(data!= null){
			addressId = data.getExtras().getString("addressId");
			peopleName = data.getExtras().getString("peopleName");
			addressStr = data.getExtras().getString("address");
			phone = data.getExtras().getString("phone");

			address.setText(addressStr);
			nickName.setText(peopleName);
			phoneNum.setText(phone);
		}

	}

}
